<?php
/**
 * User: hjd-yao
 * Date: 2018/12/14
 * Time: 11:53
 */

namespace Develop\Http\Controllers;


use App\Http\Controllers\Controller;
use App\Helpers\SelfResponse;
use App\Http\Requests\AuthRequest;
use App\Models\System\MenuModel;
use Develop\Models\AuthModel;
use Illuminate\Http\Request;

class ActionsController extends Controller
{
    private static $_authModel;
    public function __construct()
    {
        self::$_authModel = AuthModel::init();
    }

    /**
     * @title网站所有功能
     * @deprecated
     */
    public function showActions()
    {

        return view('develop::action.list_actions',[
            'page_title'=>'功能列表',
            'treeList'=>json_encode(self::$_authModel->getDTreeActions(true))
        ]);
    }

    /**
     * @title 添加功能
     * @return View
     */
    public function addActionForm()
    {
        $need_actions = self::$_authModel->getRouteList();
        $parent_info = self::$_authModel->getTreesActions();
        return view('develop::action.add_action',[
            'page_title'=>'添加功能',
            'need_actions'=>array_values($need_actions),
            'relate_actions'=>array_values(AuthModel::init()->getPostActions()),
            'parent_info'=>json_encode($parent_info),
            'menus'=>json_encode(MenuModel::init()->getTreesMenu())
        ]);
    }

    /**
     * @title 保存新增功能
     * @param AuthRequest $request
     * @return SelfResponse
     */
    public function saveAction(AuthRequest $request)
    {
        $data = $request->post();
        if(AuthModel::init()->addAction($data)){
            return SelfResponse::success('添加成功','b_Auth_showActions');
        }
        return SelfResponse::error('添加失败');
    }


    /**
     * @title 编辑功能
     * @param Request $request
     * @param $action_id
     * @return SelfResponse
     */
    public function editAction(Request $request,int $action_id)
    {

        $info = AuthModel::where('id',$action_id)->first();
        if($request->isMethod('post')){
            $data = $request->post();
            if(self::$_authModel->updateAction($data,$action_id)){
                return SelfResponse::success('更新成功','b_Auth_showActions');
            }
            return SelfResponse::error('更新失败');
        }else{
            $need_actions = self::$_authModel->getRouteList();
            $parent_info = self::$_authModel->getTreesActions();
            return view('develop::action.edit_action',[
                'page_title'=>'修改功能',
                'info'=>$info,
                'need_actions'=>array_values($need_actions),
                'relate_actions'=>array_values(AuthModel::init()->getPostActions(str2arr($info->relate_action))),
                'parent_info'=>json_encode($parent_info),
                'menus'=>json_encode(MenuModel::init()->getTreesMenu())
            ]);
        }
    }

    /**
     * @title 删除功能
     * @param int $action_id
     * @return SelfResponse
     */
    public function delete(int $action_id)
    {
        $info = self::$_authModel->where('id',$action_id)->first();
        if(!$info){
            return SelfResponse::error('查无信息');
        }
        if(self::$_authModel->where('id',$action_id)->delete()){
            self::$_authModel->whereRaw('FIND_IN_SET(?,`parent_ids`)',[$action_id])->delete();
            MenuModel::where('url',$info->action)->delete();
            return SelfResponse::success('删除成功!');
        }
        return SelfResponse::error('删除失败');
    }
}